52eb29b8cd333f15caa5406fa6d350c31b749955,src/edu/stanford/nlp/trees/international/pennchinese/ChineseGrammaticalStructure.java,ChineseGrammaticalStructure,main,#String[]#,176
Before Change
if (collapsed) {
System.out.println("------------- basic dependencies ---------------");
}
printDependencies(gs, gs.typedDependencies(true), t, conllx, false);
}
// TODO: add a non-collapsed which uses the extra dependencies
After Change
String treeDirname = props.getProperty("treeDir");
String sentFileName = props.getProperty("sentFile");
boolean conllx = props.getProperty("conllx") != null;
boolean basic = props.getProperty("basic") != null;
boolean nonCollapsed = props.getProperty("nonCollapsed") != null;
boolean collapsed = props.getProperty("collapsed") != null;
boolean parseTree = props.getProperty("parseTree") != null;
boolean keepPunct = props.getProperty("keepPunct") != null;
// force keepPunct, if conllx is turned on
if (conllx) {
keepPunct = true;
}
String hf = props.getProperty("hf");
String parserModel = props.getProperty("parserModel", "/u/nlp/data/lexparser/chineseFactored.ser.gz");
if (!basic && !collapsed) {
if (conllx) {
basic = true; // default to basic dependencies for conllx
} else {
collapsed = true; // otherwise, default to collapsed dependencies
}
}
try {
if (hf != null) {
shf = (HeadFinder)Class.forName(hf).newInstance();
System.err.println("Using "+hf);
}
} catch (Exception e) {
throw new RuntimeException("Fail to use HeadFinder: "+hf);
}
if (args.length == 0) {
System.err.printf("Usage:\n\t%s [optional flags] -treeFile treeFile\n\nOr:\n\t%s [optional flags] -sentFile sentFile\n", ChineseGrammaticalStructure.class.getName(), ChineseGrammaticalStructure.class.getName());
System.err.println("\nOptional flags:");
System.err.println("\t-parseTree : print phrase-structure parse tree");
System.err.println("\t-basic : basic non-collapsed dependencies preserving a tree structure");
System.err.println("\t-collapsed : collapsed dependencies");
System.err.println("\t-conllx : conllx formatted dependencies, can be used with either basic\n\t or collaped dependencies, but basic is recommended");
} else {
if (treeDirname != null && treeFileName != null) {
throw new RuntimeException("Only one of treeDirname or treeFileName should be set");
}
if (treeDirname != null) {
File dir = new File(treeDirname);
String[] files = dir.list();
for (String file : files) {
AddTreesFromFile(treeDirname+"/"+file, encoding, tb);
}
} else if (treeFileName != null) {
AddTreesFromFile(treeFileName, encoding, tb);
} else if (sentFileName != null) {
// Load parser by reflection, so that this class doesn't require parser for runtime use
// LexicalizedParser lp = new LexicalizedParser(parserModel);
ViterbiParserWithOptions lp;
try {
Class<?>[] classes = new Class<?>[]{String.class};
Constructor<?> constr = Class.forName("edu.stanford.nlp.parser.lexparser.LexicalizedParser").getConstructor(classes);
String[] opts = {"-retainTmpSubcategories"};
lp = (ViterbiParserWithOptions) constr.newInstance(parserModel);
lp.setOptionFlags(opts);
} catch (Exception cnfe) {
cnfe.printStackTrace();
return;
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(sentFileName));
} catch (FileNotFoundException e) {
System.err.println("Cannot find " + sentFileName);
System.exit(1);
}
try {
System.out.println("Processing sentence file " + sentFileName);
String line;
while ((line = reader.readLine()) != null) {
CHTBTokenizer chtb = new CHTBTokenizer(new StringReader(line));
List words = chtb.tokenize();
lp.parse(words);
Tree tree = lp.getBestParse();
tb.add(tree);
}
reader.close();
} catch (Exception e) {
throw new RuntimeException("Exception reading key file " + sentFileName, e);
}
}
}
for (Tree t : tb) {
Filter<String> puncFilter;
if (keepPunct) {
puncFilter = Filters.acceptFilter();
} else {
puncFilter = new ChineseTreebankLanguagePack().punctuationWordRejectFilter();
}
GrammaticalStructure gs = new ChineseGrammaticalStructure(t, puncFilter);
if (parseTree) {
System.out.println("============= parse tree =======================");
t.pennPrint();
}
//System.out.println("----------------------------");
//TreeGraph tg = new TreeGraph(t);
//System.out.println(tg);
//System.out.println("----------------------------");
//System.out.println(gs);
if (basic) {
if (collapsed || nonCollapsed) {
System.out.println("------------- basic dependencies ---------------");
}
printDependencies(gs, gs.typedDependencies(false), t, conllx, false);
}
if (nonCollapsed) {
if (basic || collapsed) {
System.out.println("------------- noncollapsed dependencies ---------------");
}
printDependencies(gs, gs.typedDependencies(true), t, conllx, false);
}
if (collapsed) {